---
title: ""
output: pdf_document
geometry: "top=1in, bottom=1in, left=1in, right=1in"
header-includes:
    - \usepackage{caption}
    - \usepackage{lscape}
---

\newcommand{\blandscape}{\begin{landscape}}
\newcommand{\elandscape}{\end{landscape}}

\captionsetup[table]{labelformat=empty}


```{r setup, include=FALSE, message=FALSE}

# Package setup
if (!require("pacman")) install.packages("pacman", repos = "http://cran.us.r-project.org"); library(pacman);
pacman::p_load(yaml, arrow, readr, tidyr, glue, argparse, here, purrr, stringr, dplyr, knitr, kableExtra);

# Working directory and command line argument setup
current_script = "generate_tables_and_figures/gen_RI_estimate_table/src/gen_RI_table.Rmd"
here::i_am(current_script)
source(here("R", "project_functions.R"))
task_dir = here(dirname(dirname(file.path(current_script))))
knitr::opts_knit$set(root.dir = task_dir)
cl_args <- parse_make_args(c(
  "RMD_FILE",
  "BASIC_REG_RESULTS_FILE",
  "BASIC_BY_SUB_REG_RESULTS_FILE",
  "RI_REG_RESULTS_FILE",
  "RI_BY_PATH_REG_RESULTS_FILE",
  "OUTCOME_LABELS_FILE",
  "OUT_DIR"
), inter_active = TRUE, task_dir = task_dir)

knitr_setup()
  
```


```{r}

main_estimates <- bind_rows(
  read_csv(cl_args$BASIC_REG_RESULTS_FILE, show_col_types = F),
  read_csv(cl_args$BASIC_BY_SUB_REG_RESULTS_FILE, show_col_types = F)) %>%
  select(outcome, subgroup, pval_itt) %>%
  filter(outcome %in% c('i_three_components', 'v_shooting_or_homicide', 'a_shooting_or_homicide', 'a_partone_non_shooting_or_homicide'))%>%
  filter(subgroup == 'full_sample' | grepl("pathway", subgroup))

ri_estimates <- bind_rows(
  read_csv(cl_args$RI_REG_RESULTS_FILE, show_col_types = F),
  read_csv(cl_args$RI_BY_PATH_REG_RESULTS_FILE, show_col_types = F)) %>%
  mutate(subgroup = case_when(!is.na(pathway) ~ glue("pathway__{pathway}"),
                              TRUE ~ 'full_sample'),
         outcome = gsub('_post_20', '', outcome),
         outcome = case_when(outcome == 'a_partone_non_shoot_hom' ~ 'a_partone_non_shooting_or_homicide',
                             TRUE ~ outcome)) %>%
  select(outcome, subgroup, ri_pvalue)

ylabels <- read_csv(cl_args$OUTCOME_LABELS_FILE, show_col_types = F) %>%
  rename(outcome = value)

source('../R/table_functions.R')
table_config <- read_yaml('../hand/tables_config.yaml')

```

```{r}

estimates = main_estimates %>%
  left_join(ri_estimates, by = c('outcome', 'subgroup'))

df_for_table <- estimates %>%
  left_join(ylabels %>% select(outcome, label, primary_component_order), by = "outcome") %>%
  mutate(subgroup_order = case_when(subgroup == 'full_sample' ~ 1,
                                    subgroup == 'pathway__ul' ~ 2,
                                    subgroup == 'pathway__cr' ~ 3,
                                    subgroup == 'pathway__re' ~ 4,)) %>%
  arrange(subgroup_order, primary_component_order) %>%
  mutate(pval_itt = fmt(pval_itt, decimals = 3),
         pval_itt_ri = fmt(ri_pvalue, decimals = 3)) %>%
  select(label, pval_itt, pval_itt_ri)

```

\blandscape

```{r}

table <- df_for_table %>%
    kable(booktabs=T, longtable=T,
          col.names = c('', 'Heteroskedasticity- robust', 'Randomization inference'),
          align=c("l", rep("c", 2)),
          linesep = '') %>%
    kable_styling(font_size = 11) %>%
    group_rows("Full sample", 1, 4) %>%
    group_rows("Algorithm pathway", 5, 8) %>%
    group_rows("Outreach pathway", 9, 12) %>%
    group_rows("Re-entry pathway", 13, 16) %>%
    add_header_above(list(" " = 1, "ITT P-value" = 2)) %>%
    column_spec(2:3, width="8em", latex_valign = 'm')

table

timestamp <- get_timestamp()
writeLines(table, glue(cl_args$OUT_DIR, "/randomization_inference_up_to_date.tex"))
writeLines(table, glue(cl_args$OUT_DIR, "/randomization_inference_{timestamp}.tex"))

```

\elandscape
